package com.marvels.financial.consum.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.Page;
import com.marvels.common.dto.PageInfoDTO;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.consum.FinancialConsumerProductApi;
import com.marvels.financial.loan.dto.CascadeChannelDto;
import com.marvels.financial.loan.dto.McptChannelDto;
import com.marvels.financial.loan.dto.McptConsultingFeeDTO;
import com.marvels.financial.loan.dto.McptProduct;
import com.marvels.financial.loan.dto.McptProductMappedDto;
import com.marvels.financial.loan.dto.McptProductType;
import com.marvels.product.param.api.ProductChannelApi;
import com.marvels.product.param.api.ProductTypeApi;
import com.marvels.product.param.service.ConsultingFeeService;
import com.marvels.product.param.service.ProductItemService;
import com.marvels.product.productmanage.service.ProductTypeService;

import cn.hutool.core.bean.BeanUtil;

@Component
public class FinancialConsumerProductImpl implements FinancialConsumerProductApi{

	@Autowired
	private ProductChannelApi productChannelApi;
	
	@Autowired
	private ProductTypeApi productTypeApi;
	
	@Autowired
	private ConsultingFeeService consultingFeeService;
	
	@Autowired
	private ProductItemService productItemService;
	
	@Autowired
	private ProductTypeService productTypeService;
	
	@Override
	public List<McptProductType> mcptProductTypeInfo(McptProductType productType) {
		try {
			com.marvels.product.param.dto.McptProductType param = new com.marvels.product.param.dto.McptProductType();
			BeanUtil.copyProperties(productType, param);
			List<com.marvels.product.param.dto.McptProductType> apiResult = productTypeApi.mcptProductTypeInfo(param);
			List<McptProductType> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptProductType.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerProductApi.mcptProductTypeInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerProductApi.mcptProductTypeInfo error", e);
			return null;
		}
	}

	@Override
	public CascadeChannelDto mcptChannelInfo(McptChannelDto mcptChannelDto) {
		try {
			com.marvels.product.productmanage.dto.McptChannelDto param = new com.marvels.product.productmanage.dto.McptChannelDto();
			BeanUtil.copyProperties(mcptChannelDto, param);
			com.marvels.product.param.dto.CascadeChannelDto apiResult = productChannelApi.mcptChannelInfo(param);
			CascadeChannelDto result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), CascadeChannelDto.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerProductApi.mcptChannelInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerProductApi.mcptChannelInfo error", e);
			return null;
		}
	}

	@Override
	public McptConsultingFeeDTO selectByProductCode(McptProduct mcptProduct) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(mcptProduct, param);
			com.marvels.product.param.dto.McptConsultingFeeDTO apiResult = consultingFeeService.selectByProductInfo(param);
			McptConsultingFeeDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McptConsultingFeeDTO.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerProductApi.mcptConsultingFeeDTO direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerProductApi.mcptConsultingFeeDTO error", e);
			return null;
		}
	}

	@Override
	public List<McptChannelDto> mcptChannelAllInfo(McptChannelDto mcptChannelDto) {
		try {
			com.marvels.product.productmanage.dto.McptChannelDto param = new com.marvels.product.productmanage.dto.McptChannelDto();
			BeanUtil.copyProperties(mcptChannelDto, param);
			List<com.marvels.product.productmanage.dto.McptChannelDto> apiResult = productChannelApi.mcptChannelAllInfo(param);
			List<McptChannelDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptChannelDto.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerProductApi.mcptChannelAllInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerProductApi.mcptChannelAllInfo error", e);
			return null;
		}
	}

	@Override
	public List<McptProductMappedDto> selectProductMappedInfo(McptProductMappedDto mcpdto) {
		try {
			com.marvels.product.productmanage.dto.McptProductMappedDto param = new com.marvels.product.productmanage.dto.McptProductMappedDto();
			BeanUtil.copyProperties(mcpdto, param);
			List<com.marvels.product.productmanage.dto.McptProductMappedDto> apiResult = productItemService.selectProductMappedInfo(param);
			List<McptProductMappedDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptProductMappedDto.class);
			MarvelsLogUtil.getInstance().info("FinancialConsumerProductApi.selectProductMappedInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerProductApi.selectProductMappedInfo error", e);
			return null;
		}
	}

	@Override
	public PageInfoDTO<McptProductType> productTypePageInfo(McptProductType productType) {
		try {
			com.marvels.product.param.dto.McptProductType param = new com.marvels.product.param.dto.McptProductType();
			BeanUtil.copyProperties(productType, param);
			Page<com.marvels.product.param.dto.McptProductType> list = productTypeApi.productTypePageInfo(param);
			PageInfoDTO<com.marvels.product.param.dto.McptProductType> listData=new PageInfoDTO<com.marvels.product.param.dto.McptProductType>();
			listData.setTotal(list.getTotal());
			listData.setList(list.getResult());
			PageInfoDTO<McptProductType> result = (PageInfoDTO<McptProductType>) 
					JSON.parseObject(JSONObject.toJSONString(listData), new TypeReference<PageInfoDTO<McptProductType>>(){});
			MarvelsLogUtil.getInstance().info("FinancialConsumerProductApi.productTypePageInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerProductApi.productTypePageInfo error", e);
			return null;
		}
	}

	@Override
	public Map<String, Object> queryProductInfo(Map<String, String> reqMap) {
		try {
			com.marvels.product.productmanage.dto.McptProductDto param = new com.marvels.product.productmanage.dto.McptProductDto();
			param.setProductType(reqMap.get("productType"));
			Map<String, Object> result = productTypeService.queryByConsultingFeeCode(param);
			MarvelsLogUtil.getInstance().info("FinancialConsumerProductApi.queryProductInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("FinancialConsumerProductApi.queryProductInfo error", e);
			return null;
		}
	}


	
	

}
